#include <time.h>
#include <stdio.h>
#include <string.h>

#include "../t4.h"

int
main(int argc, char *argv[])
{
	T4_out out;
	T4_in in;
	T4_dat dat = T4_DAT_INIT;

	(void) argc;
	(void) argv;

	if (!t4_term_set()) {
		fprintf(stderr, "Could not set up terminal for t4\n");
		return 1;
	}

	if (!t4_out_init(&out, "./pipe", 0, 1)) {
		fprintf(stderr, "Could not initialize t4_out\n");
		return 1;
	}

	t4_in_init(&in, 0);

	while (1) {
		switch (t4_in(&in, &dat)) {
		case T4_IN_NONE:
		case T4_IN_WAIT:
			break;
		case T4_IN_ERR:
			t4_dat_dispose(&dat);
			t4_out_dispose(&out);
			fprintf(stderr, "Error in t4_in\n");
			return 1;
		case T4_IN_STUFF:
			if (memchr(dat.str, 4, dat.len))
				goto end;

			t4_out(&out, &dat, NULL);
		}
	}

end:
	t4_dat_dispose(&dat);
	t4_out_dispose(&out);
	return 0;
}
